/*
panasonic.h
offer panasonic motor driver api,data type
designed by lunanting

-----------------------
2020-04-01 13:40:58
create file
-----------------------
*/

#ifndef __PANASONIC_H
#define __PANASONIC_H

#include "../../bsp/bsp.h"

#define PANS_home_offset    0   //原点偏移值
#define PANS_max_limit      10000   //最大值
#define PANS_min_limit      -10000  //最小值
//电机一圈脉冲数量
#define PANS_ROUND_PULSE  10000

//reginster address
//write cmd
#define PANS_EN_RES             0x60
#define PANS_RUN_RES            0x120
#define PANS_WARN_CLEAR_RES     0X61
#define PANS_HOME_RES           0x122
#define PANS_H_STOP_RES         0x123
#define PANS_S_STOP_RES         0x124
#define PANS_EN_STATE_RES       0x20
#define PANS_RUN_STATE_RES      0x130

//write data 0x10
#define PANS_COUNT_CLEAR_RES    0x4100  //写入0x6165 执行绝对清除
#define PANS_ERROR_CLEAR_RES    0x4102  //写入0x7274 执行报警清除
#define PANS_BLOCK_SET_RES      0x4414  //指定block number
#define PANS_BLOCK_ACT_RES      0x4415  //获取实际block number
#define PANS_BLOCK_AVL_RES      0X4416  //获取当前有效block number


#define PANS_BLOCK_SPEED0_RES   0x4600  //速度 0号块，一共有16个,地址+1
#define PANS_BLOCK_ACC0_RES1    0x4610  //加速度 0号块，一个有16个，地址+1
#define PANS_BLOCK_CMD0_RES     0x4800     //block cmd 0，一共256个，地址+2
#define PANS_BLOCK_DATA0_RES    0x4802   //block data 0，一共256个，地址+2

#define PANS_BLOCK_HOME_OFFSET_RES  0x4631  //原点偏移寄存器
#define PANS_BLOCK_MAX_LIMIT_RES    0x4633  //正向最大限位寄存器

//get motor run pulse and dir
#define xp_get_pulse(val,dir) (dir?((0x1 << 10) | val) : ((~(0x1 << 10)) & val))



/*********************data type define*********************/
//state type
typedef enum{
    pans_off=0, //关闭状态
    pans_on,    //打开状态
    pans_error  //error
}PANS_type;

//block function
typedef enum{
    block_rel=0,    //相对位置移动
    block_abs,      //绝对位置移动
    block_round,    //无线运转
    block_event,    //条件分歧时间
    block_update_speed,    //更新速度
    block_jump,     //block jump
    block_stop,     //减速停止
    block_home,     //会原点
}PANS_block_type;

//motor config
typedef struct{
    int home_offset;    //home offset
    int max_limit;      //最大正向限位
    int min_limit;      //最小负向限位
    uint16_t speed[5];    //speed0~4
    uint16_t acc[5];  //加速度0~4
    uint16_t dec[5];  //减速度0~4
}PANS_config;

//block data
typedef struct{
    u32 cmd;    //block cmd
    u32 data;   //block data
}PANS_block;

//motor encode
typedef struct{
    u32 p;  //编码器单圈编码值
    u16 r;  //多圈的圈数
    u64 h;  //合并后的编码值
}PANS_encode;

//motor state
typedef struct{
    u8 init;    //motor init state,1=init,0=uninit
    // MB_data modbus; //modbus data
    PANS_type en;  //motor enable state
    PANS_type run; //motor run state
    u8 rec_data[10];    //modbus recive buffer
    u8 rec_len; //modbus recive data length
    u8 port;    //modbus uart port
    u32 speed;   //modbus uart speed
    u8 slave_id;    //modbus slave id
    PANS_config config; //motor config
    PANS_block block[10];   //block data
}PANS_handler;




/********************function extern***********************/
extern int xp_pans_init(PANS_handler *state); //motor driver init
extern int xp_pans_config(PANS_handler *state);   //pansonic motor config
extern int xp_pans_en(PANS_handler *handler,PANS_type en); //pansonic motor enable set
extern int xp_pans_run(PANS_handler *handler,PANS_type run); //pansonic motor run set
extern int xp_pans_gohome(const PANS_handler *handler,PANS_type go); //pansonic morto back home
extern int xp_pans_fast_stop(const PANS_handler *handler,PANS_type stop);   //panasonic motor fast stop
extern int xp_pans_slow_stop(const PANS_handler *handler,PANS_type stop);   //pansonic motor slow stop
extern PANS_type xp_pans_get_en_state(const PANS_handler *handler); //get motor en state
extern PANS_type xp_pans_get_stb120_state(const PANS_handler *handler); //get motor IF stb 0x120 state
extern PANS_type xp_pans_get_run_state(const PANS_handler *handler);    //get motor run state
extern PANS_type xp_pans_get_warn_state(const PANS_handler *handler);   //get motor warn state
extern PANS_type xp_pans_get_buzy_state(const PANS_handler *handler);   //get motor buzy state
extern PANS_type xp_pans_get_en_data(const PANS_handler *handler); //get motor read en state
extern int xp_pans_clear_location(const PANS_handler *handler); //pansonic clear location
extern int xp_pans_clear_warn(const PANS_handler *handler); //clear pansonic motor warn
extern int xp_pans_set_block(const PANS_handler *handler,u16 block);    //set pansonic motor block
extern int xp_pans_set_home(const PANS_handler *handler,u32 offset);    //set panasonic motor home offset
extern int xp_pans_set_max(const PANS_handler *handler,u32 max);    //set panasonic motor max limit
extern int xp_pans_set_min_limit(const PANS_handler *handler,u32 min);  //set pansonic motor min limit
extern int xp_pans_get_actual_speed(const PANS_handler *handler);   //get pansinic actual speed
extern int xp_pans_get_encode(const PANS_handler *handler,PANS_encode *encode); //get pansonic motor encode value

extern int xp_pans_package_block(PANS_block *block,u8 cmd_code,u8 speed_code,u8 acc_code,u8 dec_code,s8 dir);  //package block cmd
extern int xp_pans_config_block(const PANS_handler *handler,const u16 addr,const u32 block);   //config pansonic motor block
extern int xp_pans_modbus_test(const PANS_handler *handler);    //panasonic motor modbus test

extern int xp_pans_config_move(PANS_handler *handler,const u8 speed_code,u8 jump_type,const u32 dis);    //config pansonic motor move block
extern int xp_pans_move_s(PANS_handler *handler,const u8 speed_code,const u32 dis); //set motor move with speed
extern int xp_pans_move(PANS_handler *handler,const u32 dis);   //set motor action a distance 
extern int xp_pans_round_block(PANS_handler *handler,const u8 speed_code,const s8 dir); //config motor round block
extern int xp_pans_confg_round( PANS_handler *handler,const u8 speed_code,const s8 dir);   //config motor round
extern int xp_pans_round(PANS_handler *handler,const u8 speed_code,const s8 dir);    //set motor round aways
extern int xp_pans_update_block(PANS_handler *handler,const u8 speed_code,const s8 dir);   //update speed
extern int xp_pans_compare_block(PANS_handler *handler,u8 save,u8 target,u8 block,u32 data);    //event touch things
extern int xp_pans_jump_block(PANS_handler *handler,u8 save_block,u8 block);  //pansonic motor jump block run
extern int xp_pans_change_speed(PANS_handler *handler,u8 speed_block,u16 speed); //set pansonic motor speed on speed block 0
extern int xp_pans_stop_block(PANS_handler *handler,u8 save,u8 type);  //config stop block

extern int xp_pans_init_block(PANS_handler *handler);   //config motor driver block
extern int xp_pans_config_speed(const PANS_handler *handler);   //config motor acc speed dec speed,5 block
extern int xp_pans_save_setting(const PANS_handler *handler);   //save setting to eeprom
extern int xp_pans_keep_synic(const PANS_handler *motor0,const PANS_handler *motor1,u32 dispartMax);    //check synic state
extern u16 xp_pans_get_error_code(const PANS_handler *motor);   //get motor error code

extern int xp_pans_set_acc(PANS_handler *motor,u16 acc,u16 dec,u8 block);   //set acc



#endif
//end of the file
